package br.caixa2.views;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JDialog;

import br.caixa2.enums.PasswordDomain;

public class ConfirmatePassword extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JButton[] buttons;
	private String selected;

	public ConfirmatePassword( String password ) {
		buttons = new JButton[5];
		init( password );
		selected = "";

	}

	private void init( String password ) {
		this.setSize( 500, 150 );
		setLayout( new GridLayout( 3, 2 ) );
		setLocationRelativeTo( null );
		setTitle( "Senha" );
		setModal( true );
		Random r = new Random();
		int location = r.nextInt( 5 );
		for ( int j = 0; j < 5; j++ ) {
			int i = r.nextInt( PasswordDomain.values().length );
			String label = PasswordDomain.values()[i].toString();
			i = r.nextInt( PasswordDomain.values().length );
			if ( location == j )
				label += "-" + password;
			else
				label += "-" + PasswordDomain.values()[i].toString();
			i = r.nextInt( PasswordDomain.values().length );
			label += "-" + PasswordDomain.values()[i].toString();
			buttons[j] = new JButton( label );
			final String button = label;
			buttons[j].addActionListener( new ActionListener() {

				@Override
				public void actionPerformed( ActionEvent arg0 ) {
					selected = button;
					ConfirmatePassword.this.setVisible( false );
				}
			} );
			add( buttons[j] );
		}
	}

	public String getSelected() {
		return selected;
	}

}